package org.xs.big_event.common;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.xs.big_event.dao.user.BaseUser;
import org.xs.big_event.dao.user.UserRepository;
import org.xs.big_event.utils.*;

import java.util.Optional;

//拦截器
@Component
public class JWT_lanJie implements HandlerInterceptor {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private TokenUtil tokenUtil;

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {

        String token= TokenUtil.getToken(request);
        if (token==null){
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            response.getWriter().write("{\"code\": 401, \"message\": \"Token不存在或格式错误\"}");
            return false;
         }

        //验证token
        if(!TokenUtil.verifyToken(token)){
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            response.getWriter().write("{\"code\": 401, \"message\": \"Token不存在或格式错误\"}");
            return false;
        }
        String user_id;
        try {
            user_id=TokenUtil.getDataFromToken(token);
            //将用户id存入线程中
            ThreadLocatUtil.set(user_id);
        }catch (Exception e){
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            response.getWriter().write("{\"code\": 401, \"message\": \"Token不存在或格式错误\"}");
            throw new RuntimeException(ResponseEnum.NO_TOKEN.getMag());
        }
        //验证token是否在redis数据库中
        if(!RedisUtil.checkToken(token)){
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            response.getWriter().write("{\"code\": 401, \"message\": \"Token不存在或格式错误\"}");
            return false;
        }
//        //验证token是否在黑名单中
//        if (TokenRmove.isTokenBlackInList(token)){
//            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
//            response.getWriter().write("{\"code\": 401, \"message\": \"Token不存在或格式错误\"}");
//            return false;
//        }

        //根据user id在数据库查询用户
        Optional<BaseUser> user= userRepository.findById(Integer.valueOf(user_id));
        //验证失败
        if (!user.isPresent()){
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            response.getWriter().write("{\"code\": 401, \"message\": \"Token不存在或格式错误\"}");
            throw new RuntimeException(ResponseEnum.TOKEN_VERIFY_ERROR.getMag());
        }
        return true;
    }
    @Override
    public void afterCompletion(HttpServletRequest request,
                                HttpServletResponse response,
                                Object handler,
                                Exception ex) throws Exception {
        //清空线程中的数据
        ThreadLocatUtil.remove();
    }
}

